﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HostingDomainWebsite;
using Obout.Grid;
using Obout.SuperForm;
using Hosting.Class;
using System.Collections;


namespace Hosting
{
    public partial class ArticleCategories : System.Web.UI.Page
    {

        private SiteMaster master;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["username"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
                return;
            }
            
            master = Page.Master as SiteMaster;
            master.setCurrentNav("ArticleCategories", "ArticleGroup");
        }

        #region [Grid inserts Article Group]
        protected void gGroup_InsertCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.ArticleGroups aGroup = new Hosting.Class.ArticleGroups();
            int isValid = aGroup.AddValid(e.Record["AGName"].ToString(), e.Record["AGDescription"].ToString());
            if (isValid == 1)
            {
                int isInsert = aGroup.Add(e.Record["AGName"].ToString(), e.Record["AGDescription"].ToString(), Boolean.Parse(e.Record["AGActive"].ToString()));
                if (isInsert == 0)
                {
                    master.getMessageSuccess("Successfully!");
                }
                else
                {
                    master.getMessageError("There is an error!");
                }
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("Article Group Name is null or empty");
            }
            else if (isValid == 11)
            {
                master.getMessageAttention("Article Group Name is greater than 50");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("Article Group Description is greater than 2000");
            }
        }
        #endregion

        #region [Grid updates Article Group]
        protected void gGroup_UpdateCommand(object sender, GridRecordEventArgs e)
        {

            Hosting.Class.ArticleGroups groups = new Hosting.Class.ArticleGroups();
            int isValid = groups.EditValid(e.Record["AGId"].ToString(), e.Record["AGName"].ToString(), e.Record["AGDescription"].ToString());
            if (isValid == 1)
            {
                int isUpdate = groups.Edit(e.Record["AGId"].ToString(), e.Record["AGName"].ToString(), e.Record["AGDescription"].ToString(), Boolean.Parse(e.Record["AGActive"].ToString()));
                if (isUpdate == 0)
                {
                    master.getMessageSuccess("Successfully!");
                }
                else
                {
                    master.getMessageError("There is an error!");
                }
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("GroupId is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("GroupId is not valid");
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("Article Group Name is null or empty");
            }
            else if (isValid == 11)
            {
                master.getMessageAttention("Article Group Name is greater than 50");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("Article Group Description is greater than 2000");
            }

        }
        #endregion

        #region [Grid deletes Article group]
        protected void gGroup_DeleteCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.ArticleGroups group = new Hosting.Class.ArticleGroups();
            var isDelete = group.delete(e.Record["AGId"].ToString());
            if (isDelete != 0)
            {
                master.getMessageError("There is an error when delete Article Group!");
            }
        }
    #endregion

    }
}